Problem Solving in Data Structures & Algorithms Using C by Hemant Jain

Problem Solving in Data Structures & Algorithms Using C by Hemant Jain

Author:Hemant Jain [Jain, Hemant]
Language: eng
Format: azw3
Publisher: UNKNOWN
Published: 2017-07-14T04:00:00+00:00


Nth Pre-Order

Solution: We want to print the node that will be at the nth index when we print the tree in PreOrder traversal. Therefore, we keep a counter to keep track of the index. When the counter is equal to index, then we print the value and return the Nth PreOrder index node.

Example 10.8:

1. treePtr NthPreOrder(treePtr root, int index)/* pre order */

2. {

3. static int counter=0;

4. treePtr temp=NULL;

5. if(root)

6. {

7. counter ++;

8. if(counter == index)

9. {

10. printf(" %d ", root->value);

11. return root;

12. }

13. temp=NthPreOrder(root->lChild,index);

14. if(temp)

15. return temp;

16. temp=NthPreOrder(root->rChild,index);

17. if(temp)

18. return temp;

19. }

20. return NULL;

21. }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.